﻿using System;
using System.Collections.Generic;
using System.Linq;
using Medianamik.Core;
using Medianamik.Core.Urls;
using SubSonic;

namespace Medianamik.Client
{
    public static class Extensions
    {
        private static char[] delimiterChars = { '.' };

        public static IEnumerable<Guid> GetParentNodeIds<T>(this ReadOnlyRecord<T> pRecord, bool inclusive,
            int? depth) where T : RecordBase<T>, new()
        {
            var path = pRecord.GetColumnValue<string>("Path");
            var nodeIds = path.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries).ToList().ConvertAll<Guid>(id => new Guid(id));

            nodeIds.Reverse();

            if (depth.HasValue && depth.Value > 0)
            {
                var numberOfItemsToRemove = (nodeIds.Count - (depth.Value + 1));

                if (numberOfItemsToRemove > 0)
                {
                    nodeIds.RemoveRange((depth.Value + 1), numberOfItemsToRemove);
                }
            }

            if (!inclusive)
                nodeIds.RemoveAt(0);

            return nodeIds;
        }

        public static IEnumerable<IUrlDictionaryEntry> SameDomainCulture(this IEnumerable<IUrlDictionaryEntry> entries,
            string cultureName)
        {
            return entries.Where(u => u.DomainCulture.EqualsIgnoreCase(cultureName));
        }
    }
}
